
const generateHexString = (length) => {
    return Array.from({length}, () => Math.floor(Math.random() * 16).toString(16)).join('');
};

const rd = (start, end) => {
    return Math.floor(Math.random() * (end - start + 1)) + start
}

// 验证身份
const verifyIdentity = async () => {

    try {
        // 1. 用户生成密钥对
        const userCrypto = new CryptoIdentity();
        await userCrypto.generateKeyPair();
        const publicKey = await userCrypto.exportPublicKey();
        

        const fabricClient = new FabricClient();
        await fabricClient.initialize();
        
        // 3. 注册身份到区块链
        const userId = 'user123';
        await fabricClient.registerIdentity(userId, publicKey, {
          role: 'member',
          organization: 'org1'
        });
        
        // 4. 创建认证挑战
        const challengeMessage = `AuthChallenge:${Date.now()}`;
        
        // 5. 用户签名挑战
        const signature = await userCrypto.signData(challengeMessage);
        
        // 6. 验证签名
        const verificationResult = await fabricClient.verifyIdentity(
          userId,
          signature,
          challengeMessage
        );
        
        if (verificationResult.valid) {
          console.log('身份认证成功!');
          console.log('用户信息:', verificationResult.identity);
        } else {
          console.log('身份认证失败');
        }
        
        // 7. 断开连接
        await fabricClient.disconnect();
      } catch (error) {
        console.error('认证流程出错:', error);
      }

    const credentialId = generateHexString(32);
    const userDid = generateHexString(32);
    
    console.log(`凭证id：${credentialId}`);
    console.log(`用户did：${userDid}`);
    

    const attributes = {
        '年龄': 25,
        '会计证书': 1,
        '研究生文凭': 1,
        '工作经验': 3
    };
    
    console.log('凭证属性：');
    Object.entries(attributes).forEach(([key, value]) => {
        console.log(`${key}=${value}`);
    });
    

    const conditions = {
        '研究生文凭': 1,
        '年龄': '30>年龄>20',
        '会计证书': 1,
        '工作经验': '>=3',
        '英语等级': null
    };
    
    console.log('验证策略条件：');
    Object.entries(conditions).forEach(([key, value]) => {
        console.log(`${key}: ${value}`);
    });
    
    console.log('撤销列表长度：50');
    console.log('验证策略数量：5');
    

    await new Promise(resolve => setTimeout(resolve, 4000+rd(2, 15)));
};

// 申请凭证
const applyCredential = async () => {

    const userId = generateHexString(32);
    const issuerDid = generateHexString(32);
    
    console.log(`用户id：${userId}`);
    console.log(`发行者did：${issuerDid}`);
    

    await new Promise(resolve => setTimeout(resolve, 79+rd(2, 5)));
    
    const credentialId = generateHexString(32);
    console.log(`凭证id：${credentialId}`);
    console.log('凭证属性数量：5');
};

// 资源交互
const resourceInteraction = async () => {
    const credentialId = generateHexString(32);
    const holderDid = generateHexString(32);
    const verifierDid = generateHexString(32);
    
    console.log(`凭证id：${credentialId}`);
    console.log(`持有者did：${holderDid}`);
    console.log(`验证者did：${verifierDid}`);
    console.log('通知验证者资源交互');
    

    await new Promise(resolve => setTimeout(resolve, Math.random() * 50));
};

module.exports = {
    verifyIdentity,
    applyCredential,
    resourceInteraction
}; 